//
//  SearchViewController.m
//  UI_Project_A
//
//  Created by 高赫 on 16/2/24.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "SearchViewController.h"
#import "HandleData.h"
#import "NewTextfield.h"
#import <DKNightVersion.h>

@interface SearchViewController ()<UITextFieldDelegate,KeyInputTextFieldDelegate>

@property (nonatomic ,retain)UIView *viewOfsearch;
@property (nonatomic ,retain)NewTextfield *textField;
@property (nonatomic ,retain)UIButton *buttonOfreturn;
@property (nonatomic ,retain)UIButton *buttonOfsearch;
@property (nonatomic ,retain)NSString *url;
@property (nonatomic ,retain)NSMutableArray *dataArray;
@property (nonatomic ,retain)NSString *soucrestring;
@property (nonatomic ,assign)NSInteger count;
@property (nonatomic ,retain)UIButton *button;

@end

@implementation SearchViewController

- (void) dealloc {

    [_viewOfsearch release];
    [_textField release];
    [_buttonOfreturn release];
    [_buttonOfsearch release];
    [_url release];
    [_soucrestring release];
    [_button release];
    
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.soucrestring = @"http://nssug.baidu.com/su?wd=";
    self.count = self.soucrestring.length;
    self.url = @"http://nssug.baidu.com/su?wd=&prod=news&ie=utf-8";
    [self craeteSearchView];
    [self createtextField];
    [self createReturnButton];
    [self createButtonOfSearch];
    [self createButton];
    
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    
    self.soucrestring = [self.soucrestring stringByAppendingString:string];
    
   self.url = [self.soucrestring stringByAppendingString:@"&prod=news&ie=utf-8"];

    
    
    return YES;
}

- (void)delegateBackWard {



}

- (void)createButton {

    
    for (int i = 0; i < 10; i++) {
        
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button.frame = CGRectMake(40, 220 + i*40, WIDTH /2, 30);
        [self.view addSubview:self.button];
        
        
        self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.button.tag = i + 1;
       
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 220 +i *40, 35, 30)];
        [self.view addSubview:label];


    }



}

- (void)make:(UIButton *)buttontype {
    
   [HandleData handleForSearchUrlString:self.url Block:^(id result) {
       

       self.dataArray = result;
       
       for (int i = 0; i < self.dataArray.count; i++) {
           
           UIButton *button = [self.view viewWithTag:i +1];
           
       [button setTitle:self.dataArray[i] forState:UIControlStateNormal];
           [button addTarget:self action:@selector(searchContect:) forControlEvents:UIControlEventTouchUpInside];
           

           self.soucrestring = @"http://nssug.baidu.com/su?wd=";

           
       }
   }];

    
}


- (void)searchContect:(UIButton *)button {
     NSString *str = @"https://m.baidu.com/from=844b/s?word=";
    NSString *str1 = [str stringByAppendingFormat:@"%@",self.dataArray[button.tag -1]];
    NSString *url = [str1 stringByAppendingString:@"&ts=0536223&t_kt=0&ie=utf-8&rsv_iqid=12626439160489059834&rsv_t=d091WV0%252FwjE9fkfUu7yZSnc1g7OOv4G0lJybPntS1qLI5gaq%252FYXO4Sli8g&sa=ib&ms=1&rsv_pq=12626439160489059834&rsv_sug4=5223&ss=001&inputT=1432"];
    NSString *urlstr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    [ [ UIApplication sharedApplication] openURL:[ NSURL URLWithString:urlstr]];
    




}

- (void)createReturnButton {

    self.buttonOfreturn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfreturn.frame = CGRectMake(20, 25, 30, 40);
    [self.viewOfsearch addSubview: self.buttonOfreturn];
    [self.buttonOfreturn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [self.buttonOfreturn addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)returnButton:(UIButton *)button {

    NSLog(@"11");
    
    
[self dismissViewControllerAnimated:YES completion:^{
    
}];



}


- (void)createButtonOfSearch {

    self.buttonOfsearch = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfsearch.frame = CGRectMake(WIDTH - 80 , 80, 50, 50);
    [self.viewOfsearch addSubview:self.buttonOfsearch];
    [self.buttonOfsearch setTitle:@"确定" forState:UIControlStateNormal];
    [self.buttonOfsearch addTarget:self action:@selector(make:) forControlEvents:UIControlEventTouchUpInside];
    
    
}



- (void)craeteSearchView {

    self.viewOfsearch = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [self.view addSubview:self.viewOfsearch];
    [self.viewOfsearch release];
    self.viewOfsearch.backgroundColor = [UIColor lightGrayColor];



}

-(void)createtextField {

    
    UIView *textField = [[UIView alloc] initWithFrame:CGRectMake(20, 80, WIDTH - 100, 50)];
    [self.viewOfsearch addSubview:textField];
    textField.backgroundColor =[UIColor lightGrayColor];
    textField.layer.masksToBounds = YES;
    textField.layer.borderColor = [UIColor whiteColor].CGColor;
    textField.layer.borderWidth = 1.0f;
    
    
    
   

   self.textField = [[NewTextfield alloc] initWithFrame:CGRectMake(40, 0, WIDTH - 140,50)];
    [textField addSubview:self.textField];
    self.textField.placeholder = @"请输入关键词";
    self.textField.backgroundColor = [UIColor lightGrayColor];
    self.textField.borderStyle = 3;
    self.textField.delegate = self;
    self.textField.keyInPutdelegate = self;
    
    UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sousuo3"]];
    imageview.frame = CGRectMake(5 ,10, 30, 30);
    [textField addSubview:imageview];

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {


    [textField resignFirstResponder];
    
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
